/*
 * Copyright 2017-2018 the original author(https://github.com/wj596)
 * 
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * </p>
 */

package net.jqsoft.common.domain;

import java.util.HashMap;
import java.util.Map;
/**
 * 
 * JSON响应
 *
 */
public class JsonResult extends HashMap<String, Object> {

	private static final long serialVersionUID = 1L;
	
	private static final String SUCCEED = "succeed"; 
	private static final String STATUS = "status";
	private static final String MESSAGE = "msg";
	private static final String DATA = "data";
	
	// 操作成功
	public static final boolean OPERATE_SUCCEED = true;
	// 操作失败
	public static final boolean OPERATE_FAILURE = false;
	// 身份验证成功
	public static final String MSG_AUTH_SUCCEED = "auth:succeed";
	// 身份验证失败
	public static final String MSG_AUTH_LOGIN_ERROR = "auth:failed";
	// 需要身份验证
	public static final String MSG_AUTH_UNAUTHORIZED = "auth:unauthorized";
	// 无权限访问
	public static final String MSG_AUTH_FORBIDDEN = "auth:forbidden";
	// 未知错误
	public static final String MSG_INTERNAL_UNKNOWN_ERROR = "internal:unknown_error";
	// 状态-成功
    public static final int SC_OK = 200;
    // 状态-错误的请求，一般用户参数格式错误，参数验证错误
    public static final int SC_BAD_REQUEST = 400;
    // 状态-需输入有效的用户名和密码
    public static final int SC_UNAUTHORIZED = 401;
    // 状态-无权限访问
    public static final int SC_FORBIDDEN = 403;
    // 状态-找不到访问的资源
    public static final int SC_NOT_FOUND = 404;
    // 状态-后台错误
    public static final int SC_INTERNAL_SERVER_ERROR = 500;

	public static JsonResult build() {
		return new JsonResult();
	}
	
	public static JsonResult ok() {
		JsonResult baseReturn = new JsonResult();
		baseReturn.put(SUCCEED, OPERATE_SUCCEED);
		baseReturn.put(STATUS, SC_OK);
		return baseReturn;
	}
	
	public static JsonResult ok(String message) {
		JsonResult baseReturn = new JsonResult();
		baseReturn.put(SUCCEED, OPERATE_SUCCEED);
		baseReturn.put(STATUS, SC_OK);
		baseReturn.put(MESSAGE, message);
		return baseReturn;
	}
	
	public static JsonResult ok(int status,String message) {
		JsonResult baseReturn = new JsonResult();
		baseReturn.put(SUCCEED, OPERATE_SUCCEED);
		baseReturn.put(STATUS, status);
		baseReturn.put(MESSAGE, message);
		return baseReturn;
	}
	
	public static JsonResult fail() {
		JsonResult baseReturn = new JsonResult();
		baseReturn.put(SUCCEED, OPERATE_FAILURE);
		baseReturn.put(STATUS, SC_INTERNAL_SERVER_ERROR);
		return baseReturn;
	}
	
	public static JsonResult fail(String message) {
		JsonResult baseReturn = new JsonResult();
		baseReturn.put(SUCCEED, OPERATE_FAILURE);
		baseReturn.put(STATUS, SC_INTERNAL_SERVER_ERROR);
		baseReturn.put(MESSAGE, message);
		return baseReturn;
	}
	
	public static JsonResult fail(int status,String message) {
		JsonResult baseReturn = new JsonResult();
		baseReturn.put(SUCCEED, OPERATE_FAILURE);
		baseReturn.put(STATUS, status);
		baseReturn.put(MESSAGE, message);
		return baseReturn;
	}
	
	public JsonResult respond(boolean respond) {
		this.put(SUCCEED, respond);
		return this;
	}
	
	public JsonResult message(String message) {
		this.put(MESSAGE, message);
		return this;
	}
	
	public JsonResult add(String key,Object value) {
		this.put(key,value);
		return this;
	}
	
	public JsonResult addAll(Map<String, ?> map) {
		this.putAll(map);
		return this;
	}
	
	public JsonResult addEntity(Map<String, ?> map) {
		this.putAll(map);
		return this;
	}
	
	public JsonResult addData(Object date) {
		this.put(DATA, date);
		return this;
	}
}